<template>
    <div class="snow-page">
        <a-card title="传感器列表">
            <template #extra>
                <a-button size="mini" type="primary" @click="addshux" style="margin-right: 20px;">新增</a-button>
                <a-button size="mini" type="primary" @click="goBack">返回</a-button>
            </template>
            <div>
                <a-table :data="tabdata" :pagination="false">
                    <template #columns>
                        <a-table-column title="ID">
                            <template #cell="{ record }">
                                {{ record.id }}
                            </template>
                        </a-table-column>
                        <a-table-column title="传感点名称">
                            <template #cell="{ record }">
                                {{ record.name }}
                            </template>
                        </a-table-column>
                        <a-table-column title="类型">
                            <template #cell="{ record }">
                                {{ record.data.iot_sensor_type }}
                            </template>
                        </a-table-column>
                        <a-table-column title="地址号">
                            <template #cell="{ record }">
                                {{ record.sensor_device_id }}
                            </template>
                        </a-table-column>
                        <a-table-column title="寄存器号">
                            <template #cell="{ record }">
                                {{ record.port_id }}
                            </template>
                        </a-table-column>
                        <a-table-column title="设备编码">
                            <template #cell="{ record }">
                                {{ record.code }}
                            </template>
                        </a-table-column>
                        <a-table-column title="指标编码">
                            <template #cell="{ record }">
                                {{ record.target_code }}
                            </template>
                        </a-table-column>
                        <a-table-column title="操作" align="center">
                            <template #cell="{ record }">
                                <a-space>
                                    <a-button size="mini" type="primary" @click="onEdit(record)">修改</a-button>
                                    <a-button size="mini" type="primary" status="danger"
                                        @click="onDelete2(record)">删除</a-button>

                                </a-space>
                            </template>
                        </a-table-column>
                    </template>
                </a-table>
                <a-pagination :total="total" style="margin-top: 10px" @change="changePageSensor" />
            </div>
        </a-card>


        <a-modal v-model:visible="visible" width="60%" :footer="false" style="z-index: 8;">
            <template #title>
                传感器添加/编辑
            </template>
            <div>
                <a-row>
                    <a-form ref="formRef" :size="form.size" :model="form" auto-label-width @submit="handleSubmit">
                        <div style="display: flex">
                            <a-col :span="12">
                                <a-form-item field="name" label="传感器名称"
                                    :rules="[{ required: true, message: '传感器名称不能为空' }]">
                                    <a-input v-model="form.name" placeholder="传感器名称" allow-clear />
                                </a-form-item>
                                <a-form-item field="sensor_device_id" label="地址号"
                                    :rules="[{ required: true, message: '地址号不能为空' }]">
                                    <a-input v-model="form.sensor_device_id" placeholder="地址号" allow-clear />
                                </a-form-item>
                                <a-form-item field="iot_sensor_type" label="传感点类型"
                                    :rules="[{ required: true, message: '传感点类型不能为空' }]">
                                    <a-select v-model="form.iot_sensor_type" placeholder="传感点类型" allow-clear
                                        allow-search>
                                        <a-option v-for="(item, index) in optinos24" :key="index" :value="item.id">
                                            {{ item.name }}
                                        </a-option>
                                    </a-select>
                                </a-form-item>
                                <a-form-item field="data_type" label="数据类型"
                                    :rules="[{ required: true, message: '数据类型不能为空' }]">
                                    <a-select v-model="form.data_type" placeholder="数据类型" allow-clear allow-search>
                                        <a-option value="0">
                                            数据属性
                                        </a-option>
                                        <a-option value="1">
                                            配置属性
                                        </a-option>
                                    </a-select>
                                </a-form-item>
                                <a-form-item field="store_strage" label="储存策略">
                                    <a-select v-model="form.store_strage" placeholder="储存策略" allow-clear allow-search>
                                        <a-option v-for="(item, index) in optinos270" :key="index" :value="item.id">
                                            {{ item.name }}
                                        </a-option>

                                    </a-select>
                                </a-form-item>
                                <a-form-item field="formula_up" label="公式处理(网关->平台)">
                                    <a-input v-model="form.formula_up" placeholder="公式计算，如 x/100 可不填" allow-clear />
                                </a-form-item>
                                <a-form-item field="target_code" label="指标编码">
                                    <a-input v-model="form.target_code" placeholder="指标编码" allow-clear  />
                                </a-form-item>
                                <!-- <a-form-item field="equip_code" label="网关编码">
                                    <a-input v-model="form.equip_code" placeholder="网关编码" allow-clear disabled />
                                </a-form-item> -->
                                <a-form-item field="code" label="设备编码"
                                    :rules="[{ required: true, message: '设备编码不能为空' }]">
                                    <a-input v-model="form.code" placeholder="设备编码" allow-clear />
                                </a-form-item>
                                <a-form-item field="target_type" label="指标类型"
                                    :rules="[{ required: true, message: '指标类型不能为空' }]">
                                    <a-select v-model="form.target_type" placeholder="指标类型" allow-clear allow-search>
                                        <a-option v-for="(item, index) in optinos602" :key="index"
                                            :value="item.dictionary_name">
                                            {{ item.name }}
                                        </a-option>
                                    </a-select>
                                </a-form-item>


                            </a-col>

                            <a-col :span="12">
                                <a-form-item field="unit" label="计量单位">
                                    <a-input v-model="form.unit" placeholder="计量单位" allow-clear />
                                </a-form-item>
                                <a-form-item field="bit_no" label="位号">
                                    <a-input v-model="form.bit_no" placeholder="位号" allow-clear />
                                </a-form-item>
                                <a-form-item field="target_describe" label="描述">
                                    <a-textarea v-model="form.target_describe" placeholder="描述" allow-clear />
                                </a-form-item>
                                <a-form-item field="sdata_degree" label="数据精确"
                                    :rules="[{ required: true, message: '数据精确不能为空' }]">
                                    <a-select v-model="form.sdata_degree" placeholder="数据精确" allow-clear allow-search>
                                        <a-option value="0">
                                            0
                                        </a-option>
                                        <a-option value="1">
                                            1
                                        </a-option>
                                        <a-option value="2">
                                            2
                                        </a-option>
                                        <a-option value="3">
                                            3
                                        </a-option>
                                        <a-option value="4">
                                            4
                                        </a-option>
                                        <a-option value="5">
                                            5
                                        </a-option>
                                        <a-option value="6">
                                            6
                                        </a-option>
                                    </a-select>
                                </a-form-item>
                                <a-form-item field="seq" label="排序" :rules="[{ required: true, message: '排序不能为空' }]">
                                    <a-input-number v-model="form.seq" placeholder="排序" allow-clear />
                                </a-form-item>
                                <a-form-item field="port_id" label="寄存器号"
                                    :rules="[{ required: true, message: '寄存器号不能为空' }]">
                                    <a-input v-model="form.port_id" placeholder="偏移量" allow-clear />
                                </a-form-item>
                                <a-form-item field="measure_unit_type" label="传感点单位"
                                    :rules="[{ required: true, message: '传感点单位不能为空' }]">
                                    <a-select v-model="form.measure_unit_type" placeholder="传感点单位" allow-clear
                                        allow-search>
                                        <a-option v-for="(item, index) in optinos20" :key="index" :value="item.id">
                                            {{ item.name }}
                                        </a-option>

                                    </a-select>
                                </a-form-item>
                                <a-form-item field="formula_down" label="公式处理(平台->网关)">
                                    <a-input v-model="form.formula_down" placeholder="公式计算，如 x*100 可不填" allow-clear />
                                </a-form-item>
                                <a-form-item field="infos" label="参数配置">
                                    <a-input-search v-model="form.infos" placeholder="参数配置" allow-clear search-button>
                                        <template #button-icon>
                                            <icon-share-alt @click="ckickcanshu" />
                                        </template>
                                    </a-input-search>
                                </a-form-item>
                            </a-col>

                        </div>
                        <div style="text-align: center; margin-left: 35%">
                            <a-form-item>
                                <a-space>
                                    <a-button type="primary" html-type="submit">提交</a-button>
                                    <a-button @click="onReset">重置</a-button>
                                </a-space>
                            </a-form-item>
                        </div>
                    </a-form>
                </a-row>
            </div>
        </a-modal>

        <a-modal v-model:visible="visible2" width="40%" :footer="false" style="z-index: 10;">
            <template #title>
                参数配置
            </template>
            <div>
                <a-form :model="form2" auto-label-width @submit="handleSubmit2">
                    <a-form-item field="types" label="属性种类" :rules="[{ required: true, message: '属性种类不能为空' }]">
                        <a-select v-model="form2.types" placeholder="属性种类" allow-clear allow-search>
                            <a-option value="1">Modbus</a-option>
                            <a-option value="2">默认</a-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item field="fcode" label="功能码" v-if="form2.types == 1"
                        :rules="[{ required: true, message: '功能码不能为空' }]">
                        <a-select v-model="form2.fcode" placeholder="功能码" allow-clear allow-search>
                            <a-option value="01">01</a-option>
                            <a-option value="02">02</a-option>
                            <a-option value="03">03</a-option>
                            <a-option value="04">04</a-option>
                            <a-option value="05">05</a-option>
                            <a-option value="06">06</a-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item field="readType" label="读写方式" v-if="form2.types == 1"
                        :rules="[{ required: true, message: '读写方式不能为空' }]">
                        <a-select v-model="form2.readType" placeholder="读写方式" allow-clear allow-search>
                            <a-option value="0">只读</a-option>
                            <a-option value="1">读写</a-option>

                        </a-select>
                    </a-form-item>
                    <a-form-item field="type" label="数据格式" v-if="form2.types == 1"
                        :rules="[{ required: true, message: '数据格式不能为空' }]">
                        <a-select v-model="form2.type" placeholder="数据格式" allow-clear allow-search>
                            <a-option value="uint">16位 无符号数</a-option>
                            <a-option value="int">16位 有符号数</a-option>
                            <a-option value="uint32">32位 无符号数</a-option>
                            <a-option value="int32">32位 有符号数</a-option>
                            <a-option value="float">32位 浮点型数</a-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item>
                        <a-button html-type="submit">确定</a-button>
                        <a-button @click="visible2 = false">取消</a-button>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script setup lang="ts">
import { Message, Modal } from "@arco-design/web-vue";
import { ref, reactive, onMounted } from 'vue';
import { getDictBycodeAPI } from "@/api/modules/dictionary/index";
import { getsensorListAPI, addsensorAPI, deletesensorAPI, updatesensorAPI } from "@/api/modules/sensor/index"
const router = useRouter();
const route = useRoute();
const node_id = ref("") as any;
const cq_equip_code = ref("") as any;
onMounted(() => {
    node_id.value = (route.query.id);
    cq_equip_code.value = (route.query.cq_equip_code);
    form.equip_code = cq_equip_code.value
    init24();
    init20();
    init270();
    init602();

    getinit();
})

const total = ref(0)
const paged = ref(1);
function changePageSensor(page) {
    paged.value = page;
    getinit();
}
async function getinit() {
    let obj = {
        paged: paged.value,
        pageSize: 10
    }
    let obj2 = { node_id: node_id.value, data_type: -1, sensor_device_id: "" }

    tabdata.value = [];
    let res = await getsensorListAPI(obj, obj2) as any;
    if (res.status == 2) {
        total.value = res.data.totalCount;
        tabdata.value = res.data.data
    }
}

const formRef = ref();
const onReset = () => {
    formRef.value.resetFields();
};

const optinos602 = ref([]);

async function init602() {
    let res = await getDictBycodeAPI(602);
    if (res.status == 2) {
        optinos602.value = res.data
    }
}

const optinos24 = ref([]);

async function init24() {
    let res = await getDictBycodeAPI(24);
    if (res.status == 2) {
        optinos24.value = res.data
    }
}
const optinos20 = ref([]);

async function init20() {
    let res = await getDictBycodeAPI(20);
    if (res.status == 2) {
        optinos20.value = res.data
    }
}

const optinos270 = ref([]);

async function init270() {
    let res = await getDictBycodeAPI(270);
    if (res.status == 2) {
        optinos270.value = res.data
    }
}
const visible = ref(false)
const visible2 = ref(false)
const form = reactive({}) as any;
const form2 = reactive({}) as any;

const tabdata = ref([]);

function addshux() {
    visible.value = true
}
function ckickcanshu() {
    visible2.value = true
}

function onEdit(recode) {
    form.name = recode.name
    form.sensor_device_id = recode.sensor_device_id
    form.iot_sensor_type = recode.iot_sensor_type
    form.data_type = recode.data_type + ''
    form.store_strage = recode.store_strage
    form.formula_up = recode.formula_up
    form.sdata_degree = recode.sdata_degree + ''
    form.seq = recode.seq
    form.port_id = recode.port_id
    form.measure_unit_type = recode.measure_unit_type
    form.formula_down = recode.formula_down
    form.infos = recode.infos
    form.target_type = recode.target_type
    form.code = recode.code
    if (form.infos) {
        let infos = JSON.parse(form.infos)
        if (infos.fcode) {
            form2.types = "1"
            form2.fcode = infos.fcode
            form2.readType = infos.readType
            form2.type = infos.type
        }
    }
    form.id = recode.id
    visible.value = true
}
const handleSubmit = async (values, errors) => {
    console.log(values);
    values.values.node_id = node_id.value;

    if (form.id) {
        let res = await updatesensorAPI(values.values) as any;
        if (res.status == 2) {
            Message.success("操作成功");
        } else {
            Message.error(res.statusMsg);
        }
    } else {
        let res = await addsensorAPI(values.values) as any;
        if (res.status == 2) {
            Message.success("操作成功");
        } else {
            Message.error(res.statusMsg);
        }
    }
    getinit();
    visible.value = false
    console.log(values);
    console.log(errors);
}
const onDelete2 = (row: any) => {
    Modal.warning({
        title: "提示",
        content: "确定删除？",
        hideCancel: false,
        closable: true,
        onOk: async () => {
            try {
                let res = (await deletesensorAPI(row.id)) as any;
                if (res.status == 2) {
                    Message.success("删除成功");
                } else {
                    Message.error(res.statusMsg);
                }
                getinit();
            } catch { }
        }
    });
};

const handleSubmit2 = (values) => {

    if (values.errors) {
        return
    }
    delete values.values.types
    form.infos = JSON.stringify(values.values)
    visible2.value = false
}
function goBack() {
    router.back();
}
</script>

<style lang="scss" scoped></style>